package com.mentaleye.controller;

import com.alibaba.fastjson.JSONObject;
import com.mentaleye.bean.EmploymentTestimonialsBean;
import com.mentaleye.biz.EmploymentTestimonialsBiz;
import com.mentaleye.util.CommonPage;
import com.mentaleye.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by hdg on 2017/6/6.
 */
@Controller
@RequestMapping(value = "/employmentTestimonials")
public class EmploymentTestimonialsController {

    @Autowired private EmploymentTestimonialsBiz employmentTestimonialsBiz;

    @RequestMapping("/index")
    public ModelAndView index(@RequestParam(value = "recollectionsid",required = false)Integer recollectionsid){
        ModelAndView view = new ModelAndView("em/EmploymentTestimonials/employmentTestimonialsList");
        return view;
    }

    @RequestMapping(value = "/listData")
    @ResponseBody
    public String EmploymentListData(CommonPage commonPage) {
        employmentTestimonialsBiz.queryCommPage(commonPage);
        System.out.println(commonPage.toSHowPage());
        return commonPage.toSHowPage();
    }

    @RequestMapping("/detail")
    public ModelAndView getEmploymentById(@RequestParam(value = "recollectionsId", required = false) Integer recollectionsid) {
        ModelAndView view = new ModelAndView("em/EmploymentTestimonials/employmentTestimonialsDetail");
        EmploymentTestimonialsBean bean = new EmploymentTestimonialsBean();
        bean.setRecollectionsId(recollectionsid);
        List<Map<String,Object>> employmentList = employmentTestimonialsBiz.query(bean);
        view.addObject("employmentList",employmentList);
        return view;
    }

    @RequestMapping(value = "/toadd")
    public ModelAndView toaddEmployment() {
        ModelAndView view = new ModelAndView("em/EmploymentTestimonials/addemploymentTestimonials");
        List<Map<String,Object>> studentList = employmentTestimonialsBiz.queryByStudent();
        view.addObject("studentList",studentList);
        view.addObject("listString", JSONObject.toJSONString(studentList));
        return view;
    }

    @RequestMapping(value = "/addData")
    @ResponseBody
    public String employmentBystudent(EmploymentTestimonialsBean  bean){
        System.out.print(bean.getRecollections());
        List<Map<String,Object>> employmentList = employmentTestimonialsBiz.query(bean);
        CommonResult result= new CommonResult();
        if(employmentList==null||employmentList.size()==0) {
            result = employmentTestimonialsBiz.add(bean);

        }
        return JSONObject.toJSONString(result);
    }

    @RequestMapping(value = "/toedit")
    public ModelAndView toeditEmployment(@RequestParam(value = "recollectionsId", required = false) Integer recollectionsid) {
        ModelAndView view = new ModelAndView("em/EmploymentTestimonials/employmentTestimonialsedit");
        EmploymentTestimonialsBean bean = new EmploymentTestimonialsBean();
        bean.setRecollectionsId(recollectionsid);
        List<Map<String,Object>> studentList = employmentTestimonialsBiz.query(bean);

        view.addObject("studentList",studentList);
        return view;
    }

    @RequestMapping(value = "/editData")
    @ResponseBody
    public String editemployment(EmploymentTestimonialsBean  bean){
        CommonResult result= employmentTestimonialsBiz.add(bean);
        return JSONObject.toJSONString(result);
    }
    @RequestMapping(value = "/delete")
    @ResponseBody
    public String employmentBystudent(@RequestParam(value = "recollectionsId", required = false) Integer recollectionsid){
        List<Integer> ids = new ArrayList<Integer>();
        ids.add(0, recollectionsid);
        return JSONObject.toJSONString(employmentTestimonialsBiz.removeList(ids));
    }
}
